<?php

namespace app\admin\controller\shop;

use app\admin\model\ShopGoods;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="shop_goods")
 */
class Goods extends AdminController
{

    use Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = ShopGoods::init();
        
    }
    protected $sort = [
        'sort' => 'desc',
        'id' => 'desc'
    ];

    protected $allowModifyFields = [
        'brief', 'price', 'sort', 'status'
    ];

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParams();
            $count = $this->model
                ->where($where)
                ->withJoin([
                    'cate','store'
                ])
                ->count();
            $list = $this->model
                ->where($where)
                ->withJoin([
                    'cate' => ['id', 'title'],
                    'store' => ['id', 'title']
                ])
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     * @param mixed $id
     * @return mixed
     */
    public function add($id = null)
    {
        if ($this->request->isAjax()) {
            $post = $this->request->except(['__token__', 'file']);
            $rule = [
                'store_id|所属店铺' => 'require',
                'goods_name|产品名称' => 'require',
                'brief|商品简介' => 'max:30',
//                'icon|分类图标' => 'require',
            ];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败，' . $e->getMessage());
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        $this->assign([
            'id' => $id,
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->except(['__token__', 'file']);
            $rule = [
                'store_id|所属店铺' => 'require',
                'goods_name|产品名称' => 'require',
                'brief|商品描述' => 'max:30',
//                'icon|分类图标' => 'require',
            ];
            $this->validate($post, $rule);
            try {
                $save = $this->model->where('id', $id)->save($post);
            } catch (\Exception $e) {
                $sql = $this->model->getLastSql();
                $this->error('保存失败，' . $e->getMessage(). $sql);
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        $this->assign([
            'id' => $id,
            'row' => $row,
        ]);
        return $this->fetch();
    }


}